package com.tyj.movie.listener;

import com.rabbitmq.client.Channel;
import com.tyj.movie.entity.MovieTheatreOrder;
import com.tyj.movie.service.MovieTheatreOrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 日期: 2021/7/14 14:13
 * 作者: 谭友杰
 * 类说明: 订单关闭
 */
@Component
@RabbitListener(queues = "close-order-queue")
public class OrderCloseListener {

    @Autowired
    MovieTheatreOrderService movieTheatreOrderService;



    @RabbitHandler
    public void receiverMessage(Message message, MovieTheatreOrder movieTheatreOrder, Channel channel) throws IOException {
        System.out.println("接受到关单操作");
        System.out.println("收到消息:string="+movieTheatreOrder);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            movieTheatreOrderService.closeOrder(movieTheatreOrder);
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            channel.basicReject(deliveryTag,true);
        }


    }
}
